package com.example.controller.backend;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.example.common.ApiResult;
import com.example.controller.UploadController;
import com.example.entity.Course;
import com.example.entity.Major;
import com.example.entity.Unit;
import com.example.service.CourseService;
import com.example.service.MajorService;
import com.example.service.UnitService;
import com.example.vo.CourseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/manager/bcourse")
@Api(tags = "课程管理")
public class BCourseController {
    @Resource
    private CourseService courseService;

    @Resource
    private MajorService majorService;

    @Resource
    private UnitService unitService;

    @Autowired
    private UploadController uploadController;


    @ApiOperation("查询课程列表")
    @GetMapping("/findAll")
    public ApiResult findAll(){
        List<Course> list = courseService.list();
        return ApiResult.ok(list);
    }

    @ApiOperation("新增课程")
    @PostMapping("/add")
    public ApiResult addCourse(Course course, @RequestPart MultipartFile upFile, HttpServletRequest request){
        ApiResult apiResult = uploadController.uploadAvatar(upFile,request);
        if (apiResult.isSuccess()==true) {
            String data = (String)apiResult.getData();
            course.setPicUrl(data);
            boolean b = courseService.save(course);
            if (b==true){
                return ApiResult.ok(b);
            }
            return ApiResult.fail("课程添加失败");
        }
        return apiResult;
    }

    @ApiOperation("更新课程")
    @PutMapping("/update")
    public ApiResult updateCourse(@RequestBody Course course){
        boolean b = courseService.updateById(course);
        if (b==true){
            return ApiResult.ok(b);
        }
        return ApiResult.fail("更新失败");
    }

    @ApiOperation("单独上传课程封面")
    @PostMapping("/updateimg")
    public ApiResult updateimg(@RequestParam(required = true,value = "id") Integer id,
                               @RequestPart(value = "upFile") MultipartFile upFile,
                               HttpServletRequest request){
        ApiResult apiResult = uploadController.uploadAvatar(upFile,request);
        if (apiResult.isSuccess() == true) {
            UpdateWrapper updateWrapper=new UpdateWrapper();
            updateWrapper.eq("id",id);
            updateWrapper.set("pic_url",apiResult.getData());
            boolean update = courseService.update(updateWrapper);
            if (update==true){
                return ApiResult.ok(apiResult.getData(),"封面上传成功");
            }else {
                return ApiResult.fail("封面上传失败");
            }
        }else {
            return apiResult;
        }
    }

    @ApiOperation("删除课程")
    @DeleteMapping("/delCourse")
    public ApiResult deleteCourse(@RequestBody Integer id){
        boolean b = courseService.removeById(id);
        if (b==true){
            return ApiResult.ok(b);
        }
        return ApiResult.fail("删除失败");
    }

    @ApiOperation("关联查询课程")
    @GetMapping("/selectVO")
    public ApiResult selectVO(){
        List<CourseVO> selectevo = courseService.selectevo();
        return ApiResult.ok(selectevo);
    }


    @ApiOperation("分页关联查询课程")
    @GetMapping("/findAllPage")
    public ApiResult findAllPage(@RequestParam(required = false,defaultValue = "") String courseName,
                                 @RequestParam(defaultValue = "1")Integer page,
                                 @RequestParam(defaultValue = "10") Integer size){
        Page<Course> itemPage = new Page<>(page, size);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("course_name",courseName);
        queryWrapper.orderByDesc("create_time");
        Page<Course> result = courseService.page(itemPage,queryWrapper);
        List<Course> records = result.getRecords();
            for (Course c:records){
                Major major = majorService.getById(c.getMajorId());
                Unit unit = unitService.getById(c.getUnitId());
                queryWrapper.eq("major_id",major);
                queryWrapper.eq("unit_id",unit);
                c.setMajor(major);
                c.setUnit(unit);
            }
        PageDTO<Course> pageDTO = new PageDTO<>();
        pageDTO.setTotal(result.getTotal());
        pageDTO.setRecords(result.getRecords());
        return ApiResult.ok(pageDTO);


    }






}
